package com.platform.event.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.platform.event.core.EventBus;
import com.platform.event.core.EventHandleHelper;
import com.platform.event.core.EventService;
import com.platform.event.core.impl.EventServiceImpl;
import com.platform.event.core.scheduler.SearchAndHandleWaitHandleEvent;
import com.platform.event.core.scheduler.SearchAndHandleWaitRetryEvent;
import com.platform.event.core.scheduler.SerchAndHandleHistoryEvent;
import com.platform.task.core.enums.TaskTypeEnum;
import com.platform.task.core.impl.ThreadPoolTaskSchedulerComponent;

@Configuration
@EnableConfigurationProperties(EventProperties.class)
public class EventAutoConfiguration {

	@Autowired
	private EventProperties eventProperties;

	@Bean
	public EventBus registerEventBus() {
		return new EventBus();
	}

	@Bean
	public ThreadPoolTaskSchedulerComponent registerEventTaskExecutor() {
		ThreadPoolTaskSchedulerComponent threadPoolTaskSchedulerComponent;
		if (eventProperties.isEnable()) {
			threadPoolTaskSchedulerComponent = ThreadPoolTaskSchedulerComponent.getInstance();
			return threadPoolTaskSchedulerComponent;
		}
		return null;
	}

	@Bean
	public SearchAndHandleWaitHandleEvent searchAndHandleWaitHandleEvent() {
		SearchAndHandleWaitHandleEvent searchAndHandleWaitHandleEvent=new SearchAndHandleWaitHandleEvent();
		registerEventTaskExecutor().registerScheduleWithFixedDelay(searchAndHandleWaitHandleEvent, 100000L,
				TaskTypeEnum.DISTRIBUTE_TASK);
		return searchAndHandleWaitHandleEvent;
	}

	@Bean
	public SearchAndHandleWaitRetryEvent searchAndHandleWaitRetryEvent() {
		SearchAndHandleWaitRetryEvent searchAndHandleWaitRetryEvent=new SearchAndHandleWaitRetryEvent();
		registerEventTaskExecutor().registerScheduleWithFixedDelay(searchAndHandleWaitRetryEvent, 100000L,
				TaskTypeEnum.DISTRIBUTE_TASK);
		return searchAndHandleWaitRetryEvent;
	}

	@Bean
	public SerchAndHandleHistoryEvent serchAndHandleHistoryEvent() {
		SerchAndHandleHistoryEvent serchAndHandleHistoryEvent=new SerchAndHandleHistoryEvent();
		registerEventTaskExecutor().registerScheduleWithFixedDelay(serchAndHandleHistoryEvent, 100000L,
				TaskTypeEnum.DISTRIBUTE_TASK);
		return serchAndHandleHistoryEvent;
	}

	@Bean
	public EventService registerEventService() {
		return new EventServiceImpl();
	}

	@Bean
	public EventHandleHelper registerEventHandleHelper() {
		return new EventHandleHelper();
	}

}
